<!DOCTYPE html><html lang="en"><head><title>OrderSample.aspx</title></head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0"><meta name="groc-relative-root" content=""><meta name="groc-document-path" content="OrderSample.aspx"><meta name="groc-project-path" content="OrderSample.aspx.cs"><link rel="stylesheet" type="text/css" media="all" href="assets/style.css"><script type="text/javascript" src="assets/behavior.js"></script><body><div id="meta"><div class="file-path">OrderSample.aspx.cs</div></div><div id="document"><div class="segment"><div class="comments "><div class="wrapper"><p>Order Sample
This sample code demonstrates how to create a new payment order.
API used: POST /v1/payments/payment</p></div></div><div class="code"><div class="wrapper"><span class="hljs-keyword">using</span> System;
<span class="hljs-keyword">using</span> PayPal.Api;
<span class="hljs-keyword">using</span> PayPal.Sample.Utilities;
<span class="hljs-keyword">using</span> System.Collections.Generic;

namespace PayPal.Sample
{
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">partial</span> <span class="hljs-keyword">class</span> OrderSample : BaseSamplePage
    {
        <span class="hljs-keyword">private</span> Order order;
        <span class="hljs-keyword">private</span> Amount amount;

        <span class="hljs-keyword">protected</span> <span class="hljs-keyword">override</span> <span class="hljs-keyword">void</span> <span class="hljs-title">RunSample</span>()
        {</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="api-context">Api Context</h3>
<p>Pass in a <code>APIContext</code> object to authenticate 
the call and to send a unique request id 
(that ensures idempotency). The SDK generates
a request id if you do not pass one explicitly. 
See <a href="/Source/Configuration.html">Configuration.cs</a> to know more about APIContext.</p></div></div><div class="code"><div class="wrapper">            <span class="hljs-keyword">var</span> apiContext = Configuration.GetAPIContext();

            <span class="hljs-keyword">string</span> payerId = Request.Params[<span class="hljs-string">"PayerID"</span>];
            <span class="hljs-keyword">if</span> (<span class="hljs-keyword">string</span>.IsNullOrEmpty(payerId))
            {</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="payer">Payer</h3>
<p>A resource representing a Payer that funds a payment
Payment Method
as <code>paypal</code></p></div></div><div class="code"><div class="wrapper">                <span class="hljs-keyword">var</span> payer = <span class="hljs-keyword">new</span> Payer() { payment_method = <span class="hljs-string">"paypal"</span> };</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h1 id="redirect-urls">Redirect URLS</h1></div></div></div><div class="segment"><div class="code"><div class="wrapper">                <span class="hljs-keyword">string</span> baseURI = Request.Url.Scheme + <span class="hljs-string">"://"</span> + Request.Url.Authority + <span class="hljs-string">"/OrderSample.aspx?"</span>;
                <span class="hljs-keyword">var</span> guid = Convert.ToString((<span class="hljs-keyword">new</span> Random()).Next(<span class="hljs-number">100000</span>));
                <span class="hljs-keyword">var</span> redirectUrl = baseURI + <span class="hljs-string">"guid="</span> + guid;
                <span class="hljs-keyword">var</span> redirUrls = <span class="hljs-keyword">new</span> RedirectUrls()
                {
                    cancel_url = redirectUrl + <span class="hljs-string">"&amp;cancel=true"</span>,
                    return_url = redirectUrl
                };</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="amount">Amount</h3>
<p>Lets you specify a payment amount.</p></div></div><div class="code"><div class="wrapper">                <span class="hljs-keyword">var</span> amount = <span class="hljs-keyword">new</span> Amount()
                {
                    currency = <span class="hljs-string">"USD"</span>,
                    total = <span class="hljs-string">"5.00"</span>
                };</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="transaction">Transaction</h3>
<p>A transaction defines the contract of a
payment - what is the payment for and who
is fulfilling it. </p></div></div><div class="code"><div class="wrapper">                <span class="hljs-keyword">var</span> transactionList = <span class="hljs-keyword">new</span> List&lt;Transaction&gt;();</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>The Payment creation API requires a list of
Transaction; add the created <code>Transaction</code>
to a List</p></div></div><div class="code"><div class="wrapper">                transactionList.Add(<span class="hljs-keyword">new</span> Transaction()
                {
                    description = <span class="hljs-string">"Transaction description."</span>,
                    amount = amount
                });</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="payment">Payment</h3>
<p>Create a payment with the intent set to &#39;order&#39;</p></div></div><div class="code"><div class="wrapper">                <span class="hljs-keyword">var</span> payment = <span class="hljs-keyword">new</span> Payment()
                {
                    intent = <span class="hljs-string">"order"</span>,
                    payer = payer,
                    transactions = transactionList,
                    redirect_urls = redirUrls
                };</div></div></div><div class="segment"><div class="code folded"><div class="wrapper marker"><span class="c1">                // Ignore workflow code segment</span></div><div class="wrapper">                <span class="hljs-comment">// Ignore workflow code segment</span>
                <span class="hljs-preprocessor">#<span class="hljs-keyword">region</span> Track Workflow</span>
                flow.AddNewRequest(<span class="hljs-string">"Create payment order"</span>, payment);
                <span class="hljs-preprocessor">#<span class="hljs-keyword">endregion</span></span></div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>Create the payment resource.</p></div></div><div class="code"><div class="wrapper">                <span class="hljs-keyword">var</span> createdPayment = payment.Create(apiContext);</div></div></div><div class="segment"><div class="code folded"><div class="wrapper marker"><span class="c1">                // Ignore workflow code segment</span></div><div class="wrapper">                <span class="hljs-comment">// Ignore workflow code segment</span>
                <span class="hljs-preprocessor">#<span class="hljs-keyword">region</span> Track Workflow</span>
                flow.RecordResponse(createdPayment);
                <span class="hljs-preprocessor">#<span class="hljs-keyword">endregion</span></span></div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>Use the <code>approval_url</code> link provided by the returned object to approve the order payment.</p></div></div><div class="code"><div class="wrapper">                <span class="hljs-keyword">var</span> links = createdPayment.links.GetEnumerator();
                <span class="hljs-keyword">while</span> (links.MoveNext())
                {
                    <span class="hljs-keyword">var</span> link = links.Current;
                    <span class="hljs-keyword">if</span> (link.rel.ToLower().Trim().Equals(<span class="hljs-string">"approval_url"</span>))
                    {
                        <span class="hljs-keyword">this</span>.flow.RecordRedirectUrl(<span class="hljs-string">"Redirect to PayPal to approve the order..."</span>, link.href);
                    }
                }
                Session.Add(<span class="hljs-string">"flow-"</span> + guid, <span class="hljs-keyword">this</span>.flow);
                Session.Add(guid, createdPayment.id);
            }
            <span class="hljs-keyword">else</span>
            {
                <span class="hljs-keyword">var</span> guid = Request.Params[<span class="hljs-string">"guid"</span>];</div></div></div><div class="segment"><div class="code folded"><div class="wrapper marker"><span class="c1">                // Ignore workflow code segment</span></div><div class="wrapper">                <span class="hljs-comment">// Ignore workflow code segment</span>
                <span class="hljs-preprocessor">#<span class="hljs-keyword">region</span> Track Workflow</span>
                <span class="hljs-keyword">this</span>.flow = Session[<span class="hljs-string">"flow-"</span> + guid] <span class="hljs-keyword">as</span> RequestFlow;
                <span class="hljs-keyword">this</span>.RegisterSampleRequestFlow();
                <span class="hljs-keyword">this</span>.flow.RecordApproval(<span class="hljs-string">"Order payment approved successfully."</span>);
                <span class="hljs-preprocessor">#<span class="hljs-keyword">endregion</span></span></div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>Execute the order</p></div></div><div class="code"><div class="wrapper">                <span class="hljs-keyword">var</span> paymentId = Session[guid] <span class="hljs-keyword">as</span> <span class="hljs-keyword">string</span>;
                <span class="hljs-keyword">var</span> paymentExecution = <span class="hljs-keyword">new</span> PaymentExecution() { payer_id = payerId };
                <span class="hljs-keyword">var</span> payment = <span class="hljs-keyword">new</span> Payment() { id = paymentId };</div></div></div><div class="segment"><div class="code folded"><div class="wrapper marker"><span class="c1">                // Ignore workflow code segment</span></div><div class="wrapper">                <span class="hljs-comment">// Ignore workflow code segment</span>
                <span class="hljs-preprocessor">#<span class="hljs-keyword">region</span> Track Workflow</span>
                flow.AddNewRequest(<span class="hljs-string">"Execute payment"</span>, payment);
                <span class="hljs-preprocessor">#<span class="hljs-keyword">endregion</span></span></div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>Execute the order payment.</p></div></div><div class="code"><div class="wrapper">                <span class="hljs-keyword">var</span> executedPayment = payment.Execute(apiContext, paymentExecution);</div></div></div><div class="segment"><div class="code folded"><div class="wrapper marker"><span class="c1">                // Ignore workflow code segment</span></div><div class="wrapper">                <span class="hljs-comment">// Ignore workflow code segment</span>
                <span class="hljs-preprocessor">#<span class="hljs-keyword">region</span> Track Workflow</span>
                flow.RecordResponse(executedPayment);
                <span class="hljs-preprocessor">#<span class="hljs-keyword">endregion</span></span></div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>Get the information about the executed order from the returned payment object.</p></div></div><div class="code"><div class="wrapper">                <span class="hljs-keyword">this</span>.order = executedPayment.transactions[<span class="hljs-number">0</span>].related_resources[<span class="hljs-number">0</span>].order;
                <span class="hljs-keyword">this</span>.amount = executedPayment.transactions[<span class="hljs-number">0</span>].amount;</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>Once the order has been executed, an order ID is returned that can be used
to do one of the following:
this.AuthorizeOrder();
this.CaptureOrder();
this.VoidOrder();
this.RefundOrder();</p></div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>For more information, please visit <a href="https://developer.paypal.com/docs/api/">PayPal Developer REST API Reference</a>.</p></div></div><div class="code"><div class="wrapper">            }
        }

        <span class="hljs-comment"><span class="hljs-xmlDocTag">///</span> <span class="hljs-xmlDocTag">&lt;summary&gt;</span></span>
        <span class="hljs-comment"><span class="hljs-xmlDocTag">///</span> Authorizes an order. This begins the process of confirming that</span>
        <span class="hljs-comment"><span class="hljs-xmlDocTag">///</span> funds are available until it is time to complete the payment</span>
        <span class="hljs-comment"><span class="hljs-xmlDocTag">///</span> transaction.</span>
        <span class="hljs-comment"><span class="hljs-xmlDocTag">///</span> </span>
        <span class="hljs-comment"><span class="hljs-xmlDocTag">///</span> More Information:</span>
        <span class="hljs-comment"><span class="hljs-xmlDocTag">///</span> https://developer.paypal.com/webapps/developer/docs/integration/direct/create-process-order/#authorize-an-order</span>
        <span class="hljs-comment"><span class="hljs-xmlDocTag">///</span> <span class="hljs-xmlDocTag">&lt;/summary&gt;</span></span>
        <span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">AuthorizeOrder</span>(APIContext apiContext)
        {
            <span class="hljs-keyword">this</span>.order.Authorize(apiContext);
        }

        <span class="hljs-comment"><span class="hljs-xmlDocTag">///</span> <span class="hljs-xmlDocTag">&lt;summary&gt;</span></span>
        <span class="hljs-comment"><span class="hljs-xmlDocTag">///</span> Captures an order. For a partial capture, you can provide a lower</span>
        <span class="hljs-comment"><span class="hljs-xmlDocTag">///</span> amount than the total payment. Additionally, you can explicitly</span>
        <span class="hljs-comment"><span class="hljs-xmlDocTag">///</span> indicate a final capture (complete the transaction and prevent</span>
        <span class="hljs-comment"><span class="hljs-xmlDocTag">///</span> future captures) by setting the is_final_capture value to true.</span>
        <span class="hljs-comment"><span class="hljs-xmlDocTag">///</span> </span>
        <span class="hljs-comment"><span class="hljs-xmlDocTag">///</span> More Information:</span>
        <span class="hljs-comment"><span class="hljs-xmlDocTag">///</span> https://developer.paypal.com/webapps/developer/docs/integration/direct/create-process-order/#capture-an-order</span>
        <span class="hljs-comment"><span class="hljs-xmlDocTag">///</span> <span class="hljs-xmlDocTag">&lt;/summary&gt;</span></span>
        <span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">CaptureOrder</span>(APIContext apiContext)
        {
            <span class="hljs-keyword">var</span> capture = <span class="hljs-keyword">new</span> Capture();
            capture.amount = <span class="hljs-keyword">this</span>.amount;
            capture.is_final_capture = <span class="hljs-keyword">true</span>;
            <span class="hljs-keyword">this</span>.order.Capture(apiContext, capture);
        }

        <span class="hljs-comment"><span class="hljs-xmlDocTag">///</span> <span class="hljs-xmlDocTag">&lt;summary&gt;</span></span>
        <span class="hljs-comment"><span class="hljs-xmlDocTag">///</span> Voids an order.</span>
        <span class="hljs-comment"><span class="hljs-xmlDocTag">///</span> </span>
        <span class="hljs-comment"><span class="hljs-xmlDocTag">///</span> NOTE: An order cannot be voided if payment has already been</span>
        <span class="hljs-comment"><span class="hljs-xmlDocTag">///</span>       partially or fully captured.</span>
        <span class="hljs-comment"><span class="hljs-xmlDocTag">///</span> </span>
        <span class="hljs-comment"><span class="hljs-xmlDocTag">///</span> More Information:</span>
        <span class="hljs-comment"><span class="hljs-xmlDocTag">///</span> https://developer.paypal.com/webapps/developer/docs/api/#void-an-order</span>
        <span class="hljs-comment"><span class="hljs-xmlDocTag">///</span> <span class="hljs-xmlDocTag">&lt;/summary&gt;</span></span>
        <span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">VoidOrder</span>(APIContext apiContext)
        {
            <span class="hljs-keyword">this</span>.order.Void(apiContext);
        }
    }
}</div></div></div></div></body></html>